package tech.aistar.corejava.day13;

import java.io.*;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * 拷贝完的文件没内容?
 * @CreateDate: 2019/8/2
 * @Version: 1.0
 */
public class BufferedDemo {
    public static void main(String[] args) {
    copyFile("src/tech/aistar/corejava/day13/FileDemo.java","src/tech/aistar/corejava/day13/FileDemo_副本.java");
    }

    private static void copyFile(String src, String target) {
        BufferedInputStream in = null;

        BufferedOutputStream out = null;

        try {
            in = new BufferedInputStream(new FileInputStream(src));
            out = new BufferedOutputStream((new FileOutputStream(target)));

            byte[] buf = new byte[3*1024];

            while(true){
                int len = in.read();

                if(len == -1){
                    break;
                }
                //强制刷新缓冲区
                out.write(buf,0,len);//?
            }
            System.out.println("文件拷贝成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //只需要关闭包装流即可
            if(null != in){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != out){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

